<?php

/**
 * Actions for Annotations
 * 
 * @author Pablo
 * @since 1.0
 */

/**
 * Append an annotation to an entiy
 * 
 * @param int $entity_guid
 * @param string $name
 * @param string $value
 * @param (integer|text) $value_type
 * @param int $owner_guid (optional, default is $entity_guid value)
 * @param int $access_id (optional, default is ACCESS_DEFAULT)
 * @param int $time_created (optional)
 * @author Pablo
 * @since 1.0
 */
function blg_annotation_add($entity_guid, $name, $value, $value_type, $owner_guid=NULL, $access_id=ACCESS_DEFAULT, $time_created= NULL){
	blg_TRANSACTION();
	
	$name_id= blg_metastring_add($name);
	$value_id= blg_metastring_add($value);
	if(empty($owner_guid)) $owner_guid= $entity_guid;
	if(empty($time_created)) $time_created= time();
	
	if(FALSE===$name_id || FALSE===$value_id){
		blg_ROLLBACK();
		return FALSE;
	}
		
	$query= blg_dbschema_query_buid(blg_dbschema_insert_annotation(array(
		'entity_guid' 	=> $entity_guid,
		'name_id' 		=> $name_id,
		'value_id' 		=> $value_id,
		'value_type' 	=> $value_type,
		'owner_guid' 	=> $owner_guid,
		'access_id' 	=> $access_id,
		'time_created'	=> $time_created,
		'enabled'		=> 'yes'
	)));
	$result= blg_dbschema_insert($query);
	
	if($result->success){
		blg_COMMIT();
		return $result->new_id;
	}
	
	blg_ROLLBACK();
	return FALSE;	
}

/**
 * Update annotation value
 * @param $id
 * @param $value
 * @return Boolean
 * @author Pablo
 * @since 1.0
 */
function blg_annotation_update_value($id, $value){
	blg_TRANSACTION();
	
	$value_id= blg_metastring_add($value);

	if(FALSE===$value_id){
		blg_ROLLBACK();
		return FALSE;
	}	
	
	$query= blg_dbschema_query_buid(blg_dbschema_update_annotations(
		array('id' => $id),
		array('value_id' => $value_id)
	));
	$result= blg_dbschema_update($query);
	
	if($result->success){
		blg_COMMIT();
		return TRUE;
	}
	
	blg_ROLLBACK();
	return FALSE;	
}

/**
 * Remove annotation by id
 * @param $id
 * @return Boolean
 * @author Pablo
 * @since 1.0
 */
function blg_delete_annotation($id){
	$query= blg_dbschema_query_buid(blg_dbschema_update_annotations(
		array('id' => $id),
		array('enabled' => 'no')
	));
	$result= blg_dbschema_update($query);
	return ($result->success && $result->affected_rows>0 );
}

/**
 * Remove entity's annotations
 * @param $id
 * @return Boolean
 * @author Pablo
 * @since 1.0
 */
function blg_delete_entity_annotations($entity_guid){
	$query= blg_dbschema_query_buid(blg_dbschema_update_annotations(
		array('entity_guid' => $entity_guid),
		array('enabled' => 'no')
	));
	$result= blg_dbschema_update($query);
	return ($result->success && $result->affected_rows>0 );
}

/**
 * Get an annotation by id
 * @param $id
 * @return (Array|FALSE) The annotation, FALSE in case of failure
 * @author Pablo
 * @since 1.0
 */
function blg_get_annotation($id){
	$query= blg_dbschema_query_buid(blg_dbschema_select_annotations(array('id' => $id)));
	$result= blg_dbschema_select($query);
	if($result->success)
		return $result->{0};		
	
	return FALSE;
}

/**
 * Retrives all annotations of an entity by name
 * @param int $entity_guid
 * @param string $name
 * @return mixed All annotations
 * @author Pablo
 * @since 1.0
 */
function blg_get_annotation_value($entity_guid, $name){
	$query= blg_dbschema_query_buid(blg_dbschema_select_annotations(
		array(
			'entity_guid' => $entity_guid,
			'name' => $name
		),
		array(
			'id'
		)
	));
	$result= blg_dbschema_select($query);
	return $result;
}






